//
//  NSObject+Extension.swift
//  Req
//
//  Created by Bernie on 16/9/23.
//  Copyright © 2016年 Bernie. All rights reserved.
//

import UIKit
func showPrompt(_ str:String){
    let error = UIControl(frame:CGRect(x: 0, y: -64, width: Int(UIScreen().width!), height: 64))
    error.backgroundColor  = UIColor(red: 235/255.0, green: 235/255.0, blue: 241/255.0, alpha: 0.8)
    let textlabel = UILabel(frame: CGRect(x: 0, y: 0, width: error.frame.size.width, height: 64))
    textlabel.textAlignment = .center
    textlabel.textColor = .default
    textlabel.text = str
    textlabel.numberOfLines = 2
    error.addSubview(textlabel)
    textlabel.isUserInteractionEnabled = false
    
    UIView.animate(withDuration: 0.3) {
        error.frame.origin.y = 0
    }
    DispatchQueue.main.after(3){
        UIView.animate(withDuration: 0.3, animations: {
            error.frame.origin.y = -64
        }, completion: { (b) in
            if b{
                error.removeFromSuperview()
            }
        })
    }
    error.tapAction = {
        UIView.animate(withDuration: 0.3, animations: {
            error.frame.origin.y = -64
        }, completion: { (b) in
            if b{
                error.removeFromSuperview()
            }
        })
    }
    if #available(iOS 13.0, *) {
        UIApplication.shared.windows.first?.addSubview(error)
    }else{
        UIApplication.shared.keyWindow?.addSubview(error)
    }
}
extension Range where Bound == String.Index{
    func nsRange(_ originString:String) -> NSRange {
        return NSRange(self, in: originString)
    }
}
extension DispatchQueue{
    /// 几秒后处理某事
    ///
    /// - Parameters:
    ///   - second: 延迟的时间（秒数），精确到小数点三位
    ///   - action: 要做的事情
    func after(_ second:Float,_ action:@escaping ()->()) {
        let millisecond = Int(second * 1000)
        let dalytime = DispatchTime.now() + DispatchTimeInterval.milliseconds(millisecond)
        self.asyncAfter(deadline: dalytime, execute: action)
    }
}
extension Dictionary where Key : Any, Value : Any{
    var anyKey:Key{
        get{
            return self.keys.first!
        }
    }
    var anyValue:Value{
        get{
            return self.values.first!
        }
    }
    mutating func replaceKey(origin:Key,target:Key){
        let c = self[origin]
        self[target] = c
        self.removeValue(forKey: origin)
    }
}


protocol ICollectionReadFile{
    static func path(with path:String) ->Self?
    static func url(with url:URL) ->Self?
}
extension Dictionary:ICollectionReadFile{
    static func path(with path: String) -> Dictionary<Key, Value>? {
        return NSDictionary(contentsOfFile: path) as? Dictionary
    }
    static func url(with url: URL) -> Dictionary<Key, Value>? {
        return NSDictionary(contentsOf: url) as? Dictionary
    }
}
extension Array:ICollectionReadFile{
    static func path(with path:String) -> Array? {
        return NSArray(contentsOfFile: path) as? Array
    }
    static func url(with url:URL) -> Array? {
        return NSArray(contentsOf: url) as? Array
    }
}
extension Dictionary{
    func reversedDictionary()->Dictionary? {
        var dic:[String:Any] = [:]
        for (key,value) in self {
            if let v = value as? String{
                dic[v] = key
            }
        }
        return dic as? Dictionary<Key, Value>
    }
    func safeObject() -> Dictionary {
        var d:[Key:Value] = [:]
        for (k,v) in self {
            if v is NSNull  {
                d[k] = "" as? Value
            }else if let _v = v as? Dictionary  {
                d[k] = _v.safeObject() as? Value
            }else if let _v = v as? Array<Dictionary>  {
                d[k] = _v.map({$0.safeObject()}) as? Value
            }else{
                d[k] = v
            }
        }
        return d
    }
}
